package servlet;

import com.alibaba.fastjson.JSON;
import pojo.UserContact;
import service.ContactService;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.List;

/**
 * 删除联系人
 * 输入一个用户联系人关联对象up，将该记录从up表中删除
 * 最后一定返回true，默认删除成功
 */
@WebServlet("/deleteContactLinkServlet")
public class DeleteContactLinkServlet extends HttpServlet {
    private final ContactService contactService = new ContactService();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        BufferedReader reader = request.getReader();
        String jsonString = reader.readLine();
        UserContact userContact = JSON.parseObject(jsonString, UserContact.class);

        List<UserContact> userContacts = contactService.selectUserContactOfUserContact(userContact);

        if (!userContacts.isEmpty())
            contactService.deleteContactLinkOfUser(userContact);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}
